package com.oocl.ita.util;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSUtil {
	private static String FACTORYURL;
	public static String QUEUE;
	static {
		Properties properties = new Properties();
		try {
			properties.load(JMSUtil.class.getResourceAsStream("/JMSConfig.properties"));
			FACTORYURL = properties.getProperty("MQFactory");
			QUEUE = properties.getProperty("QueueName");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		Connection connection = null;
		ConnectionFactory factory = new ActiveMQConnectionFactory(FACTORYURL);
		try {
			connection = factory.createConnection();
		} catch (JMSException e) {
			System.out.println("JMS Connection Error");
		}
		return connection;
	}

	public static void free(Connection con, Session session, MessageProducer p) {
		try {
			if (p != null) {
				p.close();
			}
			if (session != null) {
				session.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (JMSException e) {
			System.out.println("JMS Free Error");
		}
	}
}
